package com.kxf.bsdiff;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.kxf.bsdiff.jni.BsDiffUtil;
import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;

import rx.functions.Action1;

public class MainActivity extends AppCompatActivity {

    private String basePath = Utils.getBasePath();
    //旧版本
    private String old = basePath + "oldfile.apk";
    //新版本
    private String newp = basePath + "newfile.apk";
    //差分包
    private String patch = basePath + "file.patch";
    //旧版apk和差分包合并生成的新版apk
    private String tmp = basePath + "new.apk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //分别申请多个权限
        RxPermissions.getInstance(MainActivity.this)
                //分别申请多个权限时，使用requestEach
                .requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Action1<Permission>() {
                    @Override
                    public void call(Permission permission) {
                        Log.d("MainActivity", "permission=" + permission);
                        PackageInfo dir = Utils.getApkSourceDir(MainActivity.this, MainActivity.this.getPackageName());
                        Log.d("MainActivity",
                                "getApkSourceDir=" + dir.applicationInfo.sourceDir);
                        Log.d("MainActivity",
                                "getApkSourceDir Label=" + dir.applicationInfo.loadLabel(MainActivity.this.getPackageManager()).toString());
                    }
                });

        Log.e("MainActivity", "BsDiffUtil.getInfo()=" + BsDiffUtil.getInfo());


    }

    private void initFile(){
        {
            File oldF = new File(old);
            if (!oldF.getParentFile().exists()) {
                oldF.getParentFile().mkdirs();
            }
            if (!oldF.exists()) {
                Utils.copyAssetsToFile(this, "oldapk.tmp", old);
            }
        }

        {
            File newF = new File(newp);
            if (!newF.getParentFile().exists()) {
                newF.getParentFile().mkdirs();
            }
            if (!newF.exists()) {
                Utils.copyAssetsToFile(this, "newapk.tmp", newp);
            }
        }

    }

    public void onclickDiff(View view) {
        Log.d("Thread", "bt_diff Thread ID is " + Thread.currentThread().getId());
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("MainActivity", "newp MD5====>>>>" + Utils.getFileMD5(newp));
                long s = System.currentTimeMillis();
                Log.d("Thread", "Thread ID is " + Thread.currentThread().getId());
                BsDiffUtil.diff(old, newp, patch);
                long s1 = System.currentTimeMillis();
                Log.d("bsdiff", "生成差分包成功，用时:" + (s1 - s) + "ms");

//                Utils.printFile(patch);
            }
        }).start();
    }

    public void onclickPatch(View view) {
        Log.d("Thread", "bt_patch Thread ID is " + Thread.currentThread().getId());
        new Thread(new Runnable() {
            @Override
            public void run() {
                String newMD5 = Utils.getFileMD5(newp);
                Log.d("MainActivity", "newp MD5====>>>>" + newMD5);
                Log.d("Thread", "Thread ID is " + Thread.currentThread().getId());
                long s2 = System.currentTimeMillis();
                BsDiffUtil.patch(old, tmp, patch);
                long s3 = System.currentTimeMillis();
                Log.d("bsdiff", "差分包合并成功，用时:" + (s3 - s2) + "ms");
                String tmpMD5 = Utils.getFileMD5(tmp);
                Log.d("MainActivity", "tmp MD5====>>>>" + tmpMD5);
                Log.d("MainActivity", "校验MD5====>>>>" + (tmpMD5!=null && tmpMD5.equals(newMD5)));
//                Utils.printFile(tmp);
            }
        }).start();
    }

    public void onclickFile(View view) {
        initFile();
    }
}
